﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using TSL_Tools.SqlTools;
using ReportingEngine;
using System.IO;
using Xceed.Wpf.Toolkit.PropertyGrid;
using System.Diagnostics;
using System.Globalization;

namespace VSLT_Reporting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
      
        #region //-- IMPORT ROUTINES ----------------------------------------------------------------------
        private void ImportPerfmonCounters()
        {
            // Configure save file dialog box
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            //dlg.InitialDirectory = Properties.Settings.Default.FolderToStoreResults;
            dlg.DefaultExt = ".csv";
            dlg.Filter = "Perfmon CSV Files (.csv)|*.csv";

            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                string sqlCmd = String.Format("SELECT DISTINCT MachineName FROM LoadTestPerformanceCounterCategory WHERE LoadTestRunId = {0}", currentlyHighlightedRunId);
                TalkToSql tts = new TalkToSql(re.sqlConn, Properties.Settings.Default.ConnectionTimeout, Properties.Settings.Default.CommandTimeout);
                List<string> machines = new List<string>();
                tts.ExecuteReaderStringList(sqlCmd, out machines);

                PerfmonImportClass pis = new PerfmonImportClass(currentlyHighlightedRunId, re.sqlConn, re.connTimeout, re.cmdTimeout, machines);
                string returnValue = pis.PerformTheImport(dlg.FileName);
                ClearReportTab();
                AddQuickTextToReport(returnValue);

            }
        }
        #endregion

        #region //-- EXPORT ROUTINES ----------------------------------------------------------------------
        private void ExportPerfmonCounters()
        {
            //string str = cbPalMachine.SelectionBoxItem.ToString();
            string str = tbPalMachineName.Text;
            if (str != "")
            {
                ExportPerfmonCounters(str);
            }
            
        }

        public void ExportPerfmonCounters(string machine)
        {
            System.Windows.Input.Cursor _cursor = this.Cursor;
            this.Cursor = Cursors.Wait;

            PerfmonExportClass pes = new PerfmonExportClass(re.sqlConn, re.connTimeout, re.cmdTimeout);
            string sFileName = pes.ExportPerfmonCounters(machine, currentlyHighlightedRunId, 
                Properties.Settings.Default.FolderToStoreResults, Properties.Settings.Default.sqlConn);
            this.Cursor = _cursor;
            tsslMessage.Content = String.Format("File Created: {0}", sFileName);
        }
        #endregion
    }

}



